package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysChatRoom;
import org.dromara.system.domain.SysChatRoomFriend;
import org.dromara.system.domain.bo.SysChatRoomBo;
import org.dromara.system.domain.vo.SysChatRoomVo;
import org.dromara.system.mapper.SysChatRoomMapper;
import org.dromara.system.service.ISysChatRoomService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 聊天房间Service业务层处理
 *
 * @author Lion Li
 * @date 2025-02-25
 */
@RequiredArgsConstructor
@Service
public class SysChatRoomServiceImpl implements ISysChatRoomService {

    private final SysChatRoomMapper baseMapper;

    /**
     * 查询聊天房间
     *
     * @param id 主键
     * @return 聊天房间
     */
    @Override
    public SysChatRoomVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询聊天房间列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 聊天房间分页列表
     */
    @Override
    public TableDataInfo<SysChatRoomVo> queryPageList(SysChatRoomBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysChatRoom> lqw = buildQueryWrapper(bo);
        Page<SysChatRoomVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的聊天房间列表
     *
     * @param bo 查询条件
     * @return 聊天房间列表
     */
    @Override
    public List<SysChatRoomVo> queryList(SysChatRoomBo bo) {
        LambdaQueryWrapper<SysChatRoom> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysChatRoom> buildQueryWrapper(SysChatRoomBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysChatRoom> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SysChatRoom::getId);
        lqw.eq(bo.getLastMsgId() != null, SysChatRoom::getLastMsgId, bo.getLastMsgId());
        lqw.eq(SysChatRoom::isShowAll, bo.isShowAll());
        lqw.eq(bo.getRoomType() != null, SysChatRoom::getRoomType, bo.getRoomType());
        lqw.eq(bo.getActiveTime() != null, SysChatRoom::getActiveTime, bo.getActiveTime());
        lqw.eq(StringUtils.isNotBlank(bo.getExtraMsg()), SysChatRoom::getExtraMsg, bo.getExtraMsg());
        return lqw;
    }

    /**
     * 新增聊天房间
     *
     * @param bo 聊天房间
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysChatRoomBo bo) {
        SysChatRoom add = MapstructUtils.convert(bo, SysChatRoom.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改聊天房间
     *
     * @param bo 聊天房间
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysChatRoomBo bo) {
        SysChatRoom update = MapstructUtils.convert(bo, SysChatRoom.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysChatRoom entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除聊天房间信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void disableRoom(List<Long> asList) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'disableRoom'");
    }

    @Override
    public SysChatRoomFriend createChatRoom(Long userId, Long id) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'createChatRoom'");
    }
}
